package com.axend.aerosense.room.viewmodel;

import android.app.Activity;
import android.app.Dialog;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.network.subsciber.IProgressDialog;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.entity.RoomRemindBean;

import io.reactivex.disposables.Disposable;

/**
 * 房间Vip保存设置数据ViewModel
 * Author: ChenHongWei
 * Date: 2021/3/30 16:32
 */
public class RoomVipFunSaveDataViewModel extends BaseViewModel {

    private Disposable disposable = null;
    private Activity activity;
    //连续久坐
    public MutableLiveData<Boolean> continuousSedentary = new MutableLiveData<>(false);
    //累积久坐
    public MutableLiveData<Boolean> cumulativeSedentary = new MutableLiveData<>(false);
    private LoadingDialog loadingDialog;

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void saveData(Object bean, String url){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        disposable = EasyHttp.post(url)
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        dismissProgress();
                        ToastUtils.showShort(R.string.room_set_success);
                        activity.onBackPressed();
                    }

                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(R.string.room_set_fail);
                        dismissProgress();
                        e.printStackTrace();
                        activity.onBackPressed();
                    }
                });
    }

    /** 保存久坐提醒数据 */
    public void saveSedentaryData(RoomRemindBean continuitySitBean, RoomRemindBean accumulateSitBean){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog.Builder(activity)
                    .setMessage(activity.getString(R.string.common_loading))
                    .setCancelable(false)
                    .create();
        }

        loadingDialog.show();
        disposable = EasyHttp.post(UrlApi.URL.SetContinuitySit.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(continuitySitBean))
                .execute(new SimpleCallBack<BaseStringBean>() {
                    @Override
                    public void onTokenExpire() {
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(R.string.room_set_fail);
                        loadingDialog.dismiss();
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        setAccumulateSit(accumulateSitBean);
                    }
                });
    }

    private void setAccumulateSit(RoomRemindBean accumulateSitBean){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        disposable = EasyHttp.post(UrlApi.URL.SetAccumulateSit.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(accumulateSitBean))
                .execute(new SimpleCallBack<BaseStringBean>() {
                    @Override
                    public void onTokenExpire() {
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(R.string.room_set_fail);
                        loadingDialog.dismiss();
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        ToastUtils.showShort(R.string.room_set_success);
                        activity.onBackPressed();
                        loadingDialog.dismiss();
                    }
                });
    }


    @Override
    public void onLoad() {

    }

    @Override
    protected void onCleared() {
        if(null != disposable && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        if (null != loadingDialog){
            loadingDialog.dismiss();
            loadingDialog = null;
        }
        activity = null;
        super.onCleared();
    }
}
